package com.takepoints21.mobile;

import com.takepoints.android.TPConst;
import com.takepoints.android.services.TPServices;
import com.takepoints.android.vo.ReturnVal;
import com.takepoints.android.util.StringUtil;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class VerifyCode extends BaseActivity {
	
	private TextView lblNumber;
	private int cardId;
	private String pageSrc;
	private String promotionId;
	private String rewardNum;
	
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.verifycode);
        manageMenu(0);
        
        Bundle bundle = this.getIntent().getExtras();
        cardId = Integer.parseInt(bundle.getString("cardId"));
        pageSrc = bundle.getString("pageSrc");
        if("MyCardRewardDetail".equalsIgnoreCase(pageSrc)){
        	promotionId = bundle.getString("promotionId");
        	rewardNum = bundle.getString("rewardNum");
        }
        lblNumber = (TextView)findViewById(R.id.lblNumber);
        
        GridView gridNumPad = (GridView)findViewById(R.id.gridNumPad);
        gridNumPad.setAdapter(new NumberAdapter(this));
	}
	
	private class NumberAdapter extends BaseAdapter {
		
	    private Context context; 
	    private String[] texts = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "OK", "0", "CLR"};
	 
	    public NumberAdapter(Context context){
	        this.context = context;
	    }
	    
	    public int getCount(){
	        return texts.length;
	    }
	    
	    public Object getItem(int position){
	        return null; 
	    }
	    public long getItemId(int position){
	        return 0; 
	    }
	    
	    public View getView(int position, View convertView, ViewGroup parent){
	    	TextView tv;
	        if(convertView == null){
	            tv = new TextView(context);
	            tv.setLayoutParams(new GridView.LayoutParams(60, 60));
	        }
	        else{
	            tv = (TextView)convertView;
	        }
	        tv.setText(texts[position]);
	        tv.setBackgroundResource(R.drawable.view_info);
	        tv.setGravity(Gravity.CENTER);
	        tv.setTextSize(TypedValue.COMPLEX_UNIT_PT, 9);
	        tv.setTextColor(Color.parseColor("#FF3300"));
	        
	        final int pos = position;
	        tv.setOnClickListener(new View.OnClickListener(){
				 public void onClick(View view){
					 if("OK".equals(texts[pos])){
						 try{
							 ReturnVal retVal = null;
							 if("MyCardRewardDetail".equalsIgnoreCase(pageSrc)){
								 retVal = TPServices.rewardMyCard( getSharedIntValue(TPConst.PRF_MEMBER_ID)
				 												 , cardId, lblNumber.getText()+"", rewardNum);
							 } else if("MyCardDetail".equalsIgnoreCase(pageSrc)){
								 retVal = TPServices.useMyCard( getSharedIntValue(TPConst.PRF_MEMBER_ID)
										 					  , cardId, "MANUAL", lblNumber.getText()+"");
							 } else{
								 throw new Exception("ไม่สามารถทำรายการได้");
							 }
							 lblNumber.setText("");
							 if(retVal != null && !StringUtil.isEmpty(retVal.getTrxCode())){
								 alertResult( "กรุณาแสดงรหัสนี้แก่พนักงาน : " + retVal.getTrxCode()
										   + "\n\n(" + retVal.getTrxDesc() + ")");
							 } else{
								 alertResult("เกิดข้อผิดพลาด :\n\n" + retVal.getMessage());
							 }
						 } catch(Exception e){
							 alertResult("เกิดข้อผิดพลาดทางเทคนิค :\n\n" + e.getMessage());
						 }
					 } else if("CLR".equals(texts[pos])){
						 lblNumber.setText("");
					 } else{
						 lblNumber.setText(lblNumber.getText() + texts[pos]);
					 }
				 }
	        });
	        return tv;
	    }
	}
	
	@Override
	public void onBackPressed(){
		backToDetailPage();
		return;
	}
	
	private void alertResult(String msg){
		final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
		alertbox.setMessage(msg);
		alertbox.setPositiveButton("OK", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dlgInf, int which){
				backToDetailPage();
			}
		});
		alertbox.show();
	}
	private final void backToDetailPage(){
		Intent detailInt = null;
		if("MyCardDetail".equalsIgnoreCase(pageSrc)){
			detailInt = new Intent(this, MyCardDetail.class);
		} else if("MyCardRewardDetail".equalsIgnoreCase(pageSrc)){
			detailInt = new Intent(this, MyCardRewardDetail.class);
			detailInt.putExtra("promotionId", promotionId);
			detailInt.putExtra("rewardNum", rewardNum);
		} else if("MyOfferDetail".equalsIgnoreCase(pageSrc)){
			detailInt = new Intent(this, MyOfferDetail.class);
		} else if("MyRewardDetail".equalsIgnoreCase(pageSrc)){
			detailInt = new Intent(this, MyRewardDetail.class);
		} else{
			detailInt = new Intent(this, Takepoints.class);
		}
		detailInt.putExtra("cardId", String.valueOf(cardId));
		startActivityForResult(detailInt, 0);
    }
	
}
